package com.meixiang.dialog;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lzy.okhttputils.request.BaseRequest;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 分红Dialog
 */
public class ProfitDialogFragment extends BaseDialogFragment {

    public static String TAG = "ProfitDialogFragment";
    private String bonus_type; //分红方式
    private String fund_id;
    private String melon_method; // 分红方式 0红利再投1现金分红
    @Bind(R.id.tv_bonus_cast_bind)
    TextView tv_bonus_cast_bind; //红利再投
    @Bind(R.id.tv_cash_profit)
    TextView tv_cash_profit; //现金分红
    private LoadingDialog loadingDialog;
    private TradersPasswordtKeyDialog dialog;
    private String show_title ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            fund_id = getArguments().getString("fund_id");
            melon_method = getArguments().getString("melon_method");
            show_title = getArguments().getString("show_title");
        }
    }

    public ProfitDialogFragment() {

    }

    public static ProfitDialogFragment newInstance(Bundle bundle) {
        ProfitDialogFragment profitDialogFragment = new ProfitDialogFragment();
        profitDialogFragment.setArguments(bundle);
        return profitDialogFragment;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected int setFragmentViewId() {
        return R.layout.dialog_profit_type;
    }


    @Override
    protected void initView(View rootView) {
        loadingDialog = new LoadingDialog(getActivity());
        ButterKnife.bind(this, rootView);
    }

    @Override
    protected void initData() {
        if("1".equals(melon_method)){
            tv_cash_profit.setTextColor(ContextCompat.getColor(mActivity,R.color.fund_money_color));
        }else{
            tv_bonus_cast_bind.setTextColor(ContextCompat.getColor(mActivity,R.color.fund_money_color));
        }
    }

    @OnClick({R.id.tv_bonus_cast_bind,R.id.tv_cash_profit,R.id.tv_option})
    public void onClick(View view) {
        switch (view.getId()) {
            // 红利再投
            case R.id.tv_bonus_cast_bind:
                bonus_type = "0";
                if(bonus_type.equals(melon_method)){
                    dismiss();
                    return;
                }
                dialog = new TradersPasswordtKeyDialog(getActivity(),show_title, "", new TradersPasswordtKeyDialog.OnCustomDialogListener() {

                    @Override
                    public void getPayPassWord(TradersPasswordtKeyDialog paymentKeyDialog, String password) {
                        changeBond(password);
                    }
                });
                dismiss();
                break;
            // 现金分红
            case R.id.tv_cash_profit:
                bonus_type = "1";
                if(bonus_type.equals(melon_method)){
                    dismiss();
                    return;
                }
                dialog = new TradersPasswordtKeyDialog(getActivity(),show_title, "", new TradersPasswordtKeyDialog.OnCustomDialogListener() {

                    @Override
                    public void getPayPassWord(TradersPasswordtKeyDialog paymentKeyDialog, String password) {
                        changeBond(password);
                    }
                });
                dismiss();
                break;
            // 取消
            case R.id.tv_option:
                dismiss();
                break;
        }
    }

    /**
     * 更改分红方式
     */
    private void changeBond(String password) {
        String url = Config.FUND_BOND_CHANGE + "?fund_id=" + fund_id + "&bonus_type=" + bonus_type + "&trade_pwd=" + password;
        HttpUtils.getC(url, new HttpCallBack(getActivity()) {

            @Override
            public void onBefore(BaseRequest request) {
                if (loadingDialog != null && !loadingDialog.isShowing())
                    loadingDialog.show();
            }

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(getContext(), code);
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                if (loadingDialog != null && loadingDialog.isShowing())
                    loadingDialog.dismiss();
                dialog.dismiss();
            }

            @Override
            public void onResponse(boolean isFromCache, JSONObject jsonObject, Request request, Response response) {
                super.onResponse(isFromCache, jsonObject, request, response);
                if(jsonObject.optInt("code") == 0){
                    Tool.showTextToast(mActivity,jsonObject.optJSONObject("data").optString("err_msg"));
                }else{
                    if(jsonObject.has("data")){
                        Tool.showTextToast(mActivity,jsonObject.optJSONObject("data").optString("err_msg"));
                    }else{
                        AbToastUtil.showToast(mActivity,jsonObject.optString("err_msg"));
                    }
                }
            }
        });
    }
}
